<template>
  <div class="login-container">

    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <el-form-item prop="loginName">
        <el-input
          ref="loginName"
          v-model="loginForm.loginName"
          placeholder="loginName"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="loginPwd" class="pwdposition">
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.loginPwd"
          :type="passwordType"
          placeholder="Password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>


      <el-form-item prop="code" class="codeposition">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        />

        <div class="login-code">
          <img :src="base64Image" title="看不清,点击重新加载" class="login-code-img" @click="refreshCode">
        </div>
      </el-form-item>

      <!-- 账户类型 -->
      <el-form-item prop="accountType">
         <el-radio-group v-model="loginForm.accountType">
             <el-radio label="admin">管理员</el-radio>
             <el-radio label="merchandiser">商户</el-radio>
           </el-radio-group>
      </el-form-item>



      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登 录</el-button>

      <div class="tips">
        <router-link :to="{path:'recruit'}" target="_blank" class="el-link">商家申请入住</router-link>
      </div>

      <div class="tips_nopass">
        <router-link :to="{path:'recruitedit'}" target="_blank" class="el-link">商家入住审核不通过点这里</router-link>
      </div>

    </el-form>
  </div>
</template>

<script>
import { airth } from '@/api/captcha.js'
export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('密码长度不能小于3个字符'))
      } else {
        callback()
      }
    }
    return {
      base64Image: '',
      loginForm: {
        loginName: '13688888888',
        loginPwd: 'admin8888',
        accountType: 'admin',
        code: undefined,
        uuid: undefined
      },
      loginRules: {
        loginName: [{ required: true, trigger: 'blur' }],
        loginPwd: [{ required: true, trigger: 'blur', validator: validatePassword }],
        code: [{ required: true, trigger: 'blur' }],
        uuid: [{ required: true, trigger: 'blur' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.refreshCode()
  },
  methods: {
    // 加载验证码
    refreshCode() {
      airth().then(resp => {
        this.loginForm.uuid = resp.data.uuid
        this.base64Image = resp.data.image
      })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 点击登录按钮
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          // vuex的action方法('模块/action的方法')
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            //this.$router.push({ path: this.redirect || '/' })
            this.$router.push({ path:'/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
            //重新获取验证码
            this.refreshCode()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#000;
$cursor: #000000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  background-image: url(../../assets/bg.jpg);
  background-repeat: no-repeat;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 5px;
    color: #000;
  }

  .pwdposition{
   margin-top: 45px;
  }

  .codeposition {
    margin-top: 50px;
  }
  .login-code-img{
    width: 98%;
  }
  .login-code {
    width: 33%;
    height: 38px;
    position: absolute;
    top: 3px;
    right: 0;
    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  position:relative;

  .login-form {
   position: absolute;
   width: 375px;
   max-width: 100%;
   margin: 0 auto;
   overflow: hidden;
   top: 365px;
   left: 280px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .tips{
      background-color: #67c23a;
      color:#fff;
      padding: 12px 20px;
      font-size: 14px;
      border-radius: 4px;
      text-align:center;
    }

    .tips_nopass{
      background-color: #e6a23c;
      color:#fff;
      padding: 12px 20px;
      font-size: 14px;
      border-radius: 4px;
      text-align:center;
    }
}
</style>
